package algorithm;

public class RevenseNum {
    public int reverse(int x) {
        boolean isPositive = x >= 0 ? true : false;
        x = Math.abs(x);
        char[] c = String.valueOf(x).toCharArray();
        StringBuilder sb = new StringBuilder();
        int m = c.length - 1;
        for(int i=c.length-1;i>=0;i--){
            sb.append(c[i]);
        }
        int ret = 0;
        try {
            ret = Integer.valueOf(sb.toString());
        } catch (NumberFormatException e) {
            ret = 0;
        }
        if (isPositive) {
            return ret;
        } else {
            return -ret;
        }

//        boolean negative = x < 0;
//        if (negative) x = -x;
//        long r = 0;
//        while (x>0) {
//            r = r * 10 + x % 10;
//            x /= 10;
//        }
//        if (negative) r = -r;
//        if (r > Integer.MAX_VALUE || r < Integer.MIN_VALUE) return 0;
//        return (int)r;
    }

    public static void main(String[] args) {
        RevenseNum num = new RevenseNum();
        System.out.println(num.reverse(1999999999));
    }
}
